package com.ruifu.bom.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.bom.dto.MaterielTypeDTO;
import com.ruifu.bom.mapper.MaterielMapper;
import com.ruifu.bom.mapper.MaterielTypeMapper;
import com.ruifu.bom.mapper.MaterielVersionsMapper;
import com.ruifu.bom.service.MaterielTypeService;
import com.ruifu.bom.vo.MaterielAndVersionsVO;
import com.ruifu.bom.vo.MaterielTypeInsertFlagVO;
import com.ruifu.common.eneity.BaseEntityPlus;
import com.ruifu.common.po.Materiel;
import com.ruifu.common.po.MaterielType;
import com.ruifu.common.po.MaterielVersions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 物料业务逻辑层实现类
 *
 * @author nyr
 * @create 2019/12/19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MaterielTypeServiceImpl extends ServiceImpl<MaterielTypeMapper, MaterielType> implements MaterielTypeService {
    @Autowired(required = false)
    private MaterielTypeMapper materielTypeMapper;

    @Autowired(required = false)
    private MaterielMapper materielMapper;

    @Autowired(required = false)
    private MaterielVersionsMapper materielVersionsMapper;

    /**
     * 删除物料分类
     *
     * @param mtId 物料分类id
     * @return
     */
    @Override
    public R deleteById(Long mtId) {
        //校验是否为叶子节点
        Integer count = materielTypeMapper.selectCount(Wrappers.<MaterielType>lambdaQuery()
                .eq(MaterielType::getMtFatherId, mtId)
                .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()));
        if (count != 0) {
            return R.failed("该物料分类下还有子分类,不允许删除");
        }
        /*删除该分类下的物料*/
        //根据物料分类id获取要删除的物料id集合
        List<Long> mlIds = materielMapper.selectList(Wrappers.<Materiel>lambdaQuery()
                .eq(Materiel::getMlTypeId, mtId)
                .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()))
                .stream()
                .map(Materiel::getMlId)
                .collect(Collectors.toList());
        if (mlIds.size() != 0) {
            //批量删除物料
            materielMapper.deleteBatchIds(mlIds);
            //批量删除物料版本
            mlIds.forEach(mlId -> materielVersionsMapper.delete(Wrappers.<MaterielVersions>lambdaQuery()
                    .eq(MaterielVersions::getMvMaterielId, mlId)
                    .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()))
            );
        }
        //删除物料分类
        materielTypeMapper.deleteById(mtId);
        return R.ok("删除物料分类成功");
    }

    /**
     * 根据物料分类id查询允许添加的对象
     *
     * @param mtId 物料分类id
     * @return
     */
    @Override
    public R findAllowsAddByMtId(String mtId) {
        //物料类型计数
        Integer materielTypeCount = materielTypeMapper.selectCount(Wrappers.<MaterielType>lambdaQuery()
                .eq(MaterielType::getMtFatherId, mtId)
                .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()));
        if (materielTypeCount != 0) {
            return R.ok(new MaterielTypeInsertFlagVO(true, false));
        }
        //物料计数
        Integer materielCount = materielMapper.selectCount(Wrappers.<Materiel>lambdaQuery()
                .eq(Materiel::getMlTypeId, mtId)
                .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId()));
        if (materielCount != 0) {
            return R.ok(new MaterielTypeInsertFlagVO(false, true));
        }
        return R.ok(new MaterielTypeInsertFlagVO(true, true));
    }

    @Override
    public Page<MaterielType> findByPage(Page<MaterielType> page, MaterielTypeDTO materielTypeDTO, CloudUser cloudUser) {
        List<MaterielType> materielTypeList = materielTypeMapper.findByQuery(page, materielTypeDTO, cloudUser);
        return page.setRecords(materielTypeList);
    }


}